import * as dto from './DTO';
import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';

const baseUrl = '/parts/api/v1';

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询备货单'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询备货单详情'
});
const exportDesc = formatMessage({
    id: 'api.message.exportDesc',
    defaultMessage: '导出备货单'
});
const newOrderDesc = formatMessage({
    id: 'api.message.newOrderDesc',
    defaultMessage: '新增备货单'
});
const partSaleOrderQueryDesc = formatMessage({
    id: 'api.message.partSaleOrderQueryDesc',
    defaultMessage: '查询销售订单'
});
const getDealersDesc = formatMessage({
    id: 'api.message.getDealersDesc',
    defaultMessage: '查询总代信息'
});
const sapDesc = formatMessage({
    id: 'api.message.sapDesc',
    defaultMessage: '上传SAP'
});

//初始化
export const getInitData = () => request(`${baseUrl}/ui/stockRequirement/init`, null, dto.InitDataStruct)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getListDesc));
// 查询订单列表
export const getList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/stockRequirements?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id =>
    request(`${baseUrl}/stockRequirements/${id}`, null, dto.Detail)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));
//新增
export const newOrder = data => request(`${baseUrl}/stockRequirements`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, newOrderDesc));
// 导出
export const exportPartIns = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/stockRequirements/export?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, exportDesc))
        .then(res => res.data);
};
//查询销售订单
export const partSaleOrderQuery = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/partSaleOrders/forStockRequirement?${queryString}`, null, dto.PartSaleOrderList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, partSaleOrderQueryDesc));
};
// 快捷查询总代
export const getDealersQuickly = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forSubDealer/simple?${queryString}`, null, dto.DealerSimpleList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealersDesc));
};
// 查询总代
export const getDealers = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forSubDealer?${queryString}`, null, dto.DealerList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealersDesc));
};
//上传SAP
export const sapData = (id, data) => request(`${baseUrl}/stockRequirements/${id}/sap`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, sapDesc));
